import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:simple_circular_progress_bar/simple_circular_progress_bar.dart';
import 'package:wk_robot2/app/widget/CommonWidgets.dart';
import '../../../event/MsgContentEvent.dart';
import '../../../style/app_colors.dart';
import '../controllers/manual_connection_controller.dart';

class ManualConnectionView extends GetView<ManualConnectionController>  {
  const ManualConnectionView({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Image.asset('assets/images/ic_back2.png',width: 48.w,height: 48.h),
          onPressed: () {
             Get.back();
          },
          tooltip: MaterialLocalizations.of(context).backButtonTooltip, // 可选：设置工具提示
        ),

      ),
      body: Obx(()=>Container(
        margin: EdgeInsets.only(left: 36.w,right: 36.h,top: 4.h),
        child: controller.noFoundDevice.value ?  Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              margin: EdgeInsets.only(left: 36.w,right: 36.w),
              child:  Text('查看条形码下方的序列号后 从以下列表选择匹配的序列号',style: TextStyle(fontSize: 48.sp,color: AppColors.Page_Title_Color),),
            ),
            SizedBox(height: 150.h),
            Image.asset('assets/images/mini_search_robot_founding.webp',width: 156.w,height: 263.h,),
            SizedBox(height: 32.h),
            Text('没有找到悟空机器人',style: TextStyle(fontSize: 28.sp,color: AppColors.Page_Title_Color)),
            SizedBox(height: 360.h,),
            Container(
              width: 594.w,
              child: ElevatedButton(onPressed: () {
                controller.noFoundDevice.value = false;
                controller.toScanning();
              },
              child: Text('重新搜索',style: TextStyle(fontSize: 28.sp,color: AppColors.white_color))),
            ),
            SizedBox(height: 40.h),
            GestureDetector(
              onTap: () {
                  CommonWidgets.showDialogForAssistantHelp(context);
              },
              child: Text('帮助',style: TextStyle(fontSize: 28.sp,fontWeight: FontWeight.bold,color: AppColors.Page_Title_Color),),
            )


          ],
        ) : Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              margin: EdgeInsets.only(left: 36.w,right: 36.w),
              child:  Text('查看条形码下方的序列号后 从以下列表选择匹配的序列号',style: TextStyle(fontSize: 48.sp,color: AppColors.Page_Title_Color),),
            ),
            SizedBox(height: 33.h),
            GestureDetector(
              onTap: () {
                controller.noFoundDevice.value = false;
                controller.toScanning();
              },
              child: Container(
                child: SimpleCircularProgressBar(
                    progressStrokeWidth: 5.w,
                    backStrokeWidth:5.w,
                    size: 40.w,
                    progressColors: const [Colors.blue],
                    animationDuration: 60,
                    backColor:Colors.white
                ),
              ),
            ),
            SizedBox(height: 33.h),
            Container(
              height: 700.h,
              child: ListView.separated(
                // 提供一个唯一的 key 给每一项
                itemCount: controller.scanDevicesMap.keys.toList().length,
                itemBuilder: (context, index) {
                  String itemKey = controller.scanDevicesMap.keys.toList()[index];

                  return GestureDetector(
                    child: Container(
                      color: Colors.white.withOpacity(0),
                      padding: EdgeInsets.all(10.w),
                      child: Column(
                        children: [
                          Row(
                            children: [
                              Image.asset('assets/images/ic_avatar_online.png',width: 100.w,height: 100.h,),
                              SizedBox(width: 20.w,),
                              Text('序列号',style: TextStyle(fontSize: 26.sp,color: AppColors.line_right_character_color),),
                              SizedBox(width: 10.w,),
                              Text(itemKey,style: TextStyle(fontSize: 26.sp,color: AppColors.Page_Title_Color))

                            ],
                          ),
                          SizedBox(height: 10)

                        ],
                      ),
                    ),
                    onTap: () {
                      FlutterBluePlus.stopScan();

                      Future.delayed(Duration(milliseconds: 500), () {
                        controller.connectDeviceByManual(itemKey);
                      });
                    },
                  );
                },
                separatorBuilder: (context, index) => Divider(color: AppColors.LIST_VIEW_SEPERATED_Color,height:2.h, thickness: 0.5, indent: 6.0,endIndent: 6.0)
              ),
            ),
            SizedBox(height: 80.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset('assets/images/img_alphamini_qrcode.webp',width: 247.w,height: 289.h)
              ],
            )

          ],
        ),
      )),
    );
  }
}
